package com.cherrish.demo;

import java.util.*;

/**
 * @author cherrish
 * @time 2019-01-11 14:56
 * @name TestMap
 * @desc:
 */
public class TestMap {
    public static void main(String[] args) {
//        test1();
        test2();
    }

    private static void test2(){
        final HashMap<String, String> map = new HashMap<>(4);
        map.put("AAA", "AAA");
        map.put("BBB", "BBB");
        map.put("CCC", "CCC");
        map.put("DDD", "DDD");
        map.put("EEE", "EEE");
        System.out.println(map);
    }

    private static void test1(){
        HashMap<String,Double> map = new HashMap<String,Double>();
        ValueComparator bvc =  new ValueComparator(map);
        TreeMap<String,Double> sorted_map = new TreeMap<String,Double>(bvc);

        map.put("A",99.5);
        map.put("B",67.4);
        map.put("C",67.4);
        map.put("D",67.3);

        System.out.println("unsorted map: "+map);

        sorted_map.putAll(map);

        System.out.println("results: "+sorted_map);
    }
}



class ValueComparator implements Comparator<String> {

    Map<String, Double> base;
    public ValueComparator(Map<String, Double> base) {
        this.base = base;
    }

    @Override
    public int compare(String a, String b) {
        if (base.get(a) >= base.get(b)) {
            return -1;
        } else {
            return 1;
        } // returning 0 would merge keys
    }
}

class MapUtil{
    public static <K, V extends Comparable<? super V>> Map<K, V>
    sortByValue( Map<K, V> map ){
        List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>( map.entrySet() );
        Collections.sort( list, new Comparator<Map.Entry<K, V>>(){
            @Override
            public int compare( Map.Entry<K, V> o1, Map.Entry<K, V> o2 ){
                return (o1.getValue()).compareTo( o2.getValue() );
            }
        } );

        Map<K, V> result = new LinkedHashMap<K, V>();
        for (Map.Entry<K, V> entry : list){
            result.put( entry.getKey(), entry.getValue() );
        }
        return result;
    }
}